package com.esms.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.esms.service.ILogService;
import com.esms.service.IResponsibilityService;
import com.esms.utils.Text2Html;
import com.esms.vo.EmployeeCustomVoWithResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.esms.exception.CustomException;
import com.esms.po.Employee;
import com.esms.po.Salary;
import com.esms.po.SystemManager;
import com.esms.service.impl.UserInforServiceImpl;
import com.esms.utils.MD5Utils;
import com.esms.utils.Message;
import com.esms.vo.EmployeeCustomVo;
import com.esms.vo.EmployeeSalaryVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 个人中心Controller
 * @program: ssm
 * @Author：admin
 * @Description：
 * @Date：20:31 2021/10/6
 * @Version: 1.0
 */
@Controller
public class UserInforController {
	
    @Autowired
    private UserInforServiceImpl userInforService = null;

    @Autowired
    private ILogService logService;



    /**
     * 修改密码操作
     * @param oldPassword
     * @param newPassword
     * @param rePassword
     * @param httpSession
     * @return
     */
    @RequestMapping("changePassword.do")
    @ResponseBody
    public Map<String, String> changePassword(String oldPassword, String newPassword,
                                              String rePassword, HttpSession httpSession){
        HashMap<String, String> map = new HashMap<String, String>();
        if (newPassword.equals(rePassword)){
            SystemManager admin = (SystemManager) httpSession.getAttribute("admin");
            String encodeByMD5 = MD5Utils.encodeByMD5(oldPassword);
            if (encodeByMD5.equals(admin.getSmPassword())){
                String newPasswords = MD5Utils.encodeByMD5(newPassword);
                admin.setSmPassword(newPasswords);
                userInforService.updateSystemManagePassword(admin.getSmId(),admin);
                map.put("type","success");
                map.put("msg","密码修改成功");
                return map;
            }else{
                map.put("type","error");
                map.put("msg","原密码错误");
                return map;
            }
        }else{
            map.put("type","error");
            map.put("msg","两次密码不一致");
            return map;
        }
    }
    
    /**
     * 员工修改个人密码
     * @param oldPassword
     * @param newPassword
     * @param rePassword
     * @param httpSession
     * @return
     */
    @RequestMapping("changeEmployeePassword.do")
    @ResponseBody
    public Map<String, String> changeEmployeePassword(String oldPassword, String newPassword,
                                              String rePassword, HttpSession httpSession){
        HashMap<String, String> map = new HashMap<String, String>();
        if (newPassword.equals(rePassword)){
            Integer eid = (Integer) httpSession.getAttribute("employeeId");
            try {
                userInforService.updateEmployeePassword(eid, oldPassword, newPassword);
                map.put("type","success");
                map.put("msg","密码修改成功");
                return map;
            } catch (CustomException e) {
                map.put("type","error");
                map.put("msg","原密码错误");
                return map;
            }
        }else{
            map.put("type","error");
            map.put("msg","两次密码不一致");
            return map;
        }
    }

    /**
     * 查看个人信息
     * @param httpSession
     * @return
     */
    @RequestMapping("inforEmployee.do")
    @ResponseBody
    public  EmployeeCustomVoWithResp getInforEmployee(HttpSession httpSession){
        Integer id = (Integer) httpSession.getAttribute("employeeId");
        EmployeeCustomVoWithResp employeeCustomVo = null;
        try {
        	employeeCustomVo = userInforService.getInforEmployee(id);
        }catch(Exception e) {
        	e.printStackTrace();
        }
        assert employeeCustomVo != null;
        employeeCustomVo.setJobResponsibility(Text2Html.TextToHtml(employeeCustomVo.getJobResponsibility()));
        return employeeCustomVo;
    }

    /**
     * 修改个人信息
     * @param httpSession
     * @param employee
     * @return
     */
    @ResponseBody
    @RequestMapping("updateInforEmployee.do")
    public Message updateInforEmployee(HttpSession httpSession, Employee employee){
        Integer id = (Integer) httpSession.getAttribute("employeeId");
        employee.seteId(id);
        if(userInforService.updateEmploueeById(id,employee)<=0) {
            logService.addLog(this.getClass().getName(),Thread.currentThread().getStackTrace()[1].getMethodName(),(Integer) httpSession.getAttribute("employeeId"),
                    "修改信息失败");
        	return Message.error("修改信息失败");
        }
        logService.addLog(this.getClass().getName(),Thread.currentThread().getStackTrace()[1].getMethodName(),(Integer)httpSession.getAttribute("employeeId"),
                "修改个人信息成功");
       return Message.success();
    }



    /**
     * 个人工资信息
     * @param pageNum
     * @param limit
     * @param year
     * @param httpSession
     * @return
     * @throws Exception
     */
    @RequestMapping("employeeSalaryList.do")
    @ResponseBody
    public EmployeeSalaryVO findSelective(
            @RequestParam(value="page", defaultValue="1")int pageNum,
            @RequestParam(value="limit", defaultValue="10") int limit,
            @RequestParam(value="year", defaultValue="1") String year,
            HttpSession httpSession) throws Exception {

        Integer eId = (Integer) httpSession.getAttribute("employeeId");
        //pageNum:起始页面  pageSize:每页的大小
        PageHelper.startPage(pageNum,limit);
        //查找条件，一定要紧跟在startPage后
        List<Salary> salaryList = userInforService.getEmployeeSalaryList(eId, year);

        PageInfo pageResult = new PageInfo(salaryList);

        //设置前台需要的数据
        EmployeeSalaryVO employeeSalaryVO = new EmployeeSalaryVO();
        employeeSalaryVO.setCode(0);
        employeeSalaryVO.setMsg("");
        employeeSalaryVO.setCount((int) pageResult.getTotal());
        employeeSalaryVO.setData(pageResult.getList());
        return employeeSalaryVO;
    }

}
